package gobase

import (
	"fmt"
	"strings"
	"time"
)

func HashStr(str string) int64 {
	var sum int64 = 0
	for i := 0; i < len(str); i++ {
		sum += int64(str[i])
	}
	return sum
}

func Split2Str(s, sep string) (s1, s2 string) {
	if len(s) == 0 {
		return
	}
	idx := strings.Index(s, sep)
	if idx == -1 {
		return s, ""
	}
	return s[:idx], s[idx+len(sep):]
}

func LastSplit2Str(s, sep string) (s1, s2 string) {
	if len(s) == 0 {
		return
	}
	idx := strings.LastIndex(s, sep)
	if idx == -1 {
		return s, ""
	}
	return s[:idx], s[idx+len(sep):]
}

func GetStrValue(val interface{}, def string) string {
	switch vv := val.(type) {
	case []byte:
		return string(vv)
	case string:
		return vv
	case int:
		return fmt.Sprintf("%d", vv)
	case int64:
		return fmt.Sprintf("%d", vv)
	case int8:
		return fmt.Sprintf("%d", vv)
	case int16:
		return fmt.Sprintf("%d", vv)
	case int32:
		return fmt.Sprintf("%d", vv)
	case float64:
		return fmt.Sprintf("%f", vv)
	case float32:
		return fmt.Sprintf("%f", vv)
	case time.Time:
		return DateTimeString2(vv)
	default:
		return def
	}
}
